import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    vip:false,
    checked:false, //全选状态
    checkedArr:[],//被勾选商品的id的数组
    sL:null, //自营商品购物车个数
    JL:null, //京东商品购物车个数
    goods:[],
    list:[
      {
        title:"自营商品",
        goods: [
          {
            id: '1',
            title: '蕉下 冰薄系列防晒围巾 【初桃粉】',
            desc: '初桃粉',
            vipPrice:46,
            price: 79,
            num: 1,
            imgSrc:
              'https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/9bddd43bcb40b1464104f4f91b808b22.png',
          },
          {
            id: '2',
            title: 'CXCX初吻口红',
            desc: 'CXCX初吻口红',
            vipPrice:298,
            price: 358,
            num: 1,
            imgSrc:
              'https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/cdd250fd3a14c4f549205370aae4019f.png',
          },
          {
            id: '3',
            title: 'YUMC男士背包学生书包电脑旅行双肩背包休闲时尚B2085',
            desc: 'YUMC男士背包学生书包电脑旅行双肩背包休闲时尚B2085',
            vipPrice:229,
            price: 259,
            num: 1,
            imgSrc:
              'https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/1ae1a03f92d926079a12d28a4a9b4a1f.jpg',
          },
        ],
      },
      // {
      //   title:"京东商品",
      //   goods: [
      //     {
      //       id: '1',
      //       title: '蕉下 冰薄系列防晒围巾 【初桃粉】',
      //       desc: '初桃粉',
      //       vipPrice:46,
      //       price: 79,
      //       num: 1,
      //       imgSrc:
      //         'https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/9bddd43bcb40b1464104f4f91b808b22.png',
      //     },
      //     {
      //       id: '2',
      //       title: 'CXCX初吻口红',
      //       desc: 'CXCX初吻口红',
      //       vipPrice:298,
      //       price: 358,
      //       num: 1,
      //       imgSrc:
      //         'https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/cdd250fd3a14c4f549205370aae4019f.png',
      //     },
      //   ],
      // },
    ],
    totalPrice: 0,
    checkedGoodsNum:0,
    vipSaveMoney:'0.00',
    edit:true,
    active:0,
    title:"自营商店"
  },

  changeEdit(){
    this.setData({
      edit:!this.data.edit
    })
  },

  //商品数量修改
  onChangeNum(e){
    const num=e.detail;
    const { id }=e.currentTarget.dataset;
    let i=this.data.goods.findIndex(item=>item.id==id);
    let temp=`goods[${i}].num`;
    this.setData({
      [temp]:num
    },()=>{
      const idArr=this.data.checkedArr;
      const goods=this.data.goods;
      let priceObj=this.getSelectPrice(idArr,goods)
      const {totalPrice,vipSaveMoney}=priceObj;
      this.setData({
        totalPrice,
        vipSaveMoney
      })
    })
  },

  //全选
  onCheckedAll(e){
    let checkedArr=this.data.goods.map(item=>item.id)
    let checkedGoodsNum=checkedArr.length;
    this.setData({
      checked:!this.data.checked,
      checkedArr,
      checkedGoodsNum,
    },()=>{
      const idArr=this.data.checkedArr;
      const goods=this.data.goods;
      let priceObj=this.getSelectPrice(idArr,goods);
      const {totalPrice,vipSaveMoney}=priceObj;
      this.setData({
        totalPrice,
        vipSaveMoney
      })
    })
    if(!this.data.checked){
      this.setData({
        checkedArr:[],
        checkedGoodsNum:0
      })
    }
  },
//单选
  onChange(e){
    const checkedGoodsNum=e.detail.length;
    const idArr=e.detail;
    const goods=this.data.goods;
    let priceObj=this.getSelectPrice(idArr,goods)
    const {totalPrice,vipSaveMoney}=priceObj
    this.setData({
      checkedArr:e.detail,
      checkedGoodsNum,
      totalPrice,
      vipSaveMoney
    })
    if(e.detail.length===this.data.goods.length){
      this.setData({
        checked:true
      })
    }else{
      this.setData({
        checked:false
      })
    }
  },

  //提交订单
  onSubmitOrder(){
    const idArr=this.data.checkedArr;
    if(this.data.edit){
      //提交订单
      //将商品id发送，然后跳转到订单页
      //判断是否有选中的商品
      if(this.data.checkedArr.length){
        wx.navigateTo({
          url: '/pages/orderDetails/orderDetails',
        })
      }else{
        Toast("请先选择商品")
      }
    }else{
      //删除订单中选中的商品
      console.log(idArr)
      const goods=this.data.goods
      idArr.map(item=>{
        let i=goods.findIndex(good=>good.id==item)
        goods.splice(i,1)
      });
      console.log(goods)
      // this.setData({
      //   goods
      // })
    }
  },

  //点击免费领取跳转页面
  goRedeemVip(){
    wx.navigateTo({
      url: '/pages/vip/index',
    })
  },

  //获取被选中的商品总价以及优惠价格
  getSelectPrice(idArr,goods){
    let priceArr=[]
    idArr.map(item=>{
      goods.map(good=>{
        if(good.id==item){
          let obj={
            vipPrice:good.vipPrice,
            price:good.price,
            num:good.num
          }
          priceArr.push(obj)
        }
      })
    })
    let totalPrice=0,allPrice=0,vipMoney=0,vipSaveMoney=0;
    priceArr.map(item=>{
      allPrice+=(item.num*item.price);
      vipMoney+=(item.num*item.vipPrice);
      vipSaveMoney=allPrice-vipMoney;
      if(this.data.vip){
        totalPrice=vipMoney*100;
      }else{
        totalPrice=allPrice*100;
      }
    })
    return {totalPrice,vipSaveMoney}
  },

  onChangeTab(e){
    const i=e.detail.index;
    const goods=this.data.list[i].goods;
    const title=this.data.list[i].title;
    this.setData({
      goods,
      title,
      checkedArr:[],
      checked:false
    })
  },

  onLoad: function() {
    //获取用户状态判断是否为vip，若为VIP，修改this.data.vip的值

    //初始化购物车商品列表
    const sL=this.data.list[0];
    const JL=this.data.list[1] ? this.data.list[1] : '';
    const goods=this.data.list[0].goods;
    this.setData({
      sL,
      JL,
      goods
    })
  },

});